<?php

class Controller extends AbstractController
{

  protected $action;

  protected $name;

  protected $path;

  protected $route;

  protected $template;

  protected $layout;

  /**
   *
   * @return Controller
   */
  public static function factory($params)
  {
    Simplify::config()->set('in_admin', $params->get('in_admin'));

    $name = $params['controller'];

    $base = APP_DIR . '/controllers';

    $path = ($params->get('in_admin') ? '/admin' : '') . ($params['path'] ? '/' . $params['path'] : '');
    $filename = $base . $path . '/' . $name . '_controller.php';

    if (! file_exists($filename)) {
      $path = ($params->get('in_admin') ? '/admin' : '') . ($params['path'] ? '/' . $params['path'] : '') . '/' . $name;
      $filename = $base . $path . '/' . $name . '_controller.php';
    }

    if (! file_exists($filename)) {
      throw new Exception("Could not factory controller: file not found (".var_export($params->getAll(), true).")");
    }

    require_once($filename);

    $class = Inflector::camelize($name) . 'Controller';

    if (! class_exists($class)) {
      throw new Exception("Could not factory controller: class not found");
    }

    $Controller = new $class();
    $Controller->setPath($path);

    return $Controller;
  }

  protected function afterAction($action, $output)
  {
    //
  }

  protected function beforeAction($action)
  {
    //
  }

  protected function indexAction()
  {
    //
  }

  public function getAction()
  {
    return $this->action;
  }

  public function getName()
  {
    if (empty($this->name)) {
      $this->name = strtolower(substr(get_class($this), 0, strpos(get_class($this), 'Controller')));
    }

    return $this->name;
  }

  public function getPath()
  {
    return $this->path;
  }

  public function getRoute()
  {
    return $this->route;
  }

  public function getLayout()
  {
    global $config;

    if ((is_null($this->layout) && $this->request->ajax()) || $this->layout === false) {
      return false;
    }

    elseif (empty($this->layout)) {
      $layout = $config['views_dir'] . '/layouts/default.php';
    }

    elseif (sy_path_is_absolute($this->layout)) {
      $layout = $this->layout;
    }

    else {
      $layout = $config['views_dir'] . '/layouts/' . $this->layout . '.php';
    }

    return $layout;
  }

  public function getTemplate()
  {
    global $config;

    if ($this->template === false) {
      return false;
    }

    elseif (empty($this->template)) {
      $template = $config['views_dir'] . $this->getPath() . '/' . $this->getName() . '_' . $this->getAction() . '_view.php';
    }

    elseif (sy_path_is_absolute($this->template)) {
      $template = $this->template;
    }

    else {
      $template = $config['views_dir'] . $this->getPath() . '/' . $this->template . '_view.php';
    }

    return $template;
  }

  public function setPath($path)
  {
    $this->path = $path;
  }

  public function setLayout($layout = null)
  {
    $this->layout = $layout;
  }

  public function setTemplate($template)
  {
    $this->template = $template;
  }

}
